<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2016/9/2
 * Time: 11:26
 */


use yii\helpers\Url;
$web = Url::base();
$this->title = '卡片管理';
?>

<script type="application/javascript">
    var from_institution;
    var to_institution;
    $(function ($) {
        var institution =  <?= json_encode($institutions) ?>;
        from_institution = $('#from_institution').treeAutocomplete({
            defaultText: '发卡机构',
            source: institution,
            onSelected: function (element) {
            }
        });


        <?php
        if($from_institution_code != ''){
        ?>
        from_institution.setValue('<?=$from_institution_code?>');
        <?php
        }
        ?>

//        from_institution.setValue('');
//        from_institution.getValue();
//        from_institution.getText();

        to_institution = $('#to_institution').treeAutocomplete({
            defaultText: '收卡机构',
            source: institution,
            onSelected: function (element) {
            }
        });
        <?php
        if($to_institution_code != ''){
        ?>
        to_institution.setValue('<?=$to_institution_code?>');
        <?php
        }
        ?>

        $('#myModalEdit').modal({
            keyboard: false, backdrop: 'static', show: false
        });
        $('#myModalDetail').modal({
            keyboard: false, backdrop: 'static', show: false
        });
    });

    function filter() {
        var card_type = $("#card_type").val();
        var from_institution_code = from_institution.getValue();
        var to_institution_code = to_institution.getValue();
        var startTime = $("#startTime").val();
        var endTime = $("#endTime").val();
        var card_name = $("#card_name").val();
        var voucher = $("#voucher").val();

        var href = window.location.href;
        var index = href.indexOf("?page=");
        if (index > 0) {
            href = href.substr(0, index);
        }
        var href_str = href + "?page=1";

        if (card_type != '' && card_type != null) {
            href_str = href_str + "&card_type=" + card_type;
        }

        if (from_institution_code != '' && from_institution_code != null) {
            href_str = href_str + "&from_institution_code=" + from_institution_code;
        }

        if (to_institution_code != '' && to_institution_code != null) {
            href_str = href_str + "&to_institution_code=" + to_institution_code;
        }

        if (startTime != '' && startTime != null) {
            href_str = href_str + "&startTime=" + startTime;
        }

        if (endTime != '' && endTime != null) {
            href_str = href_str + "&endTime=" + endTime;
        }

        if (card_name != '' && card_name != null) {
            href_str = href_str + "&name=" + card_name;
        }

        if (voucher != '' && voucher != null) {
            href_str = href_str + "&voucher=" + voucher;
        }

        window.location.href = href_str;
    }

    function onAddData(id) {
        $("#myModalEdit").modal('show');
        var data = {};
        if (id != undefined) {
            data.id = id;
        }
        modalEdit(data);
    }

    //打开新增修改页面
    function modalEdit(data) {
        $("#modaledit").html('');
        loading.show();
        requestHtml('card-part-view', data, true, function (responseHtml) {
            $("#modaledit").html(responseHtml);
            loading.hide();
        });
    }

    //打开卡片单证
    function getCardVoucher(card_id) {
        $("#myModalCardVoucher").modal('show');
        var data = {};
        data.card_id = card_id;
        $("#modalCardVoucher").html('');
        requestHtml('card-part-view-voucher', data, true, function (responseHtml) {
            $("#modalCardVoucher").html(responseHtml);
        });
    }

    //新增/修改
    function saveData() {
        var id = $("#modaledit_submit_form").find("#id").val();
        var to_institution_code = to_institution_sel.getValue();
        var name = $("#modaledit_submit_form").find("#name").val();
        var description = $("#modaledit_submit_form").find("#description").val();
        var start_index = $("#modaledit_submit_form").find("#start_index").val();
        var end_index = $("#modaledit_submit_form").find("#end_index").val();
        var integral = $("#modaledit_submit_form").find("#integral").val();
        var activationVoucher = $("#modaledit_submit_form").find("#activationVoucher").is(":checked");

        var goods = {};
        goods.list = [];
        var goodsChecks = $("#goods_table").find('input:checkbox[name=goods]:checked');
        for (var i = 0; i < goodsChecks.length; i++) {
            var g = {};
            g.code = $(goodsChecks[i]).val();
            g.sort = $(goodsChecks[i]).parent().parent().find('input').val();
            goods.list.push(g);
        }

        if (to_institution_code == '' || to_institution_code == null) {
            $("#hint_message").text('收卡机构不能为空！');
            $("#hintMessageModal").modal('show');
            return false;
        }

        if (name == '' || name == null) {
            $("#hint_message").text('卡片名称不能为空！');
            $("#hintMessageModal").modal('show');
            return false;
        }

        if (start_index == '' || start_index == null) {
            $("#hint_message").text('单证开始不能为空！');
            $("#hintMessageModal").modal('show');
            return false;
        }

        if (end_index == '' || end_index == null) {
            $("#hint_message").text('单证结束不能为空！');
            $("#hintMessageModal").modal('show');
            return false;
        }

        if (end_index - start_index < 0) {
            $("#hint_message").text('单证结束不能小于单证开始为空！');
            $("#hintMessageModal").modal('show');
            return false;
        }

        if (integral == '' || integral == null) {
            $("#hint_message").text('价值积分不能为空！');
            $("#hintMessageModal").modal('show');
            return false;
        }

        if (goods.list.length <= 0) {
            $("#hint_message").text('请选择卡片模板产品！');
            $("#hintMessageModal").modal('show');
            return false;
        }

        var data = {};
        data.id = id;
        data.to_institution_code = to_institution_code;
        data.name = name;
        data.description = description;
        data.start_index = start_index;
        data.end_index = end_index;
        data.integral = integral;
        data.activationVoucher = activationVoucher;
        data.goodslist = goods.list;

        loading.show();

        //如果是我的卡片进行检查单证起始是否同一张卡片
        var my_card_id = $("#my_card_sel").val();
        if (my_card_id != undefined && my_card_id != "") {
            if (!checkCardVoucher(my_card_id, start_index, end_index)) {
                return false;
            }
        }

        requestJson("card-save-handler", data, true, function (response) {
            if (response.status == 0) {
                filter();
            } else {
                $("#hint_message").text(response.message);
                $("#hintMessageModal").modal('show');
            }
            loading.hide();
        });
    }

    function checkCardVoucher(my_card_id, start_index, end_index) {
        var result = false;
        var datac = {};
        datac.card_id = my_card_id;
        datac.start_index = start_index;
        datac.end_index = end_index;
        requestJson("check-card-voucher-handler", datac, false, function (response) {
            if (response.status != 0) {
                $("#hint_message").text(response.message);
                $("#hintMessageModal").modal('show');
                return false;
            }
            result = true;
        });

        return result;
    }

    function getDetail(card_id) {
        $("#myModalDetail").modal('show');
        var data = {};
        data.card_id = card_id;
        $("#modalDetail").html('');
        loading.show();
        requestHtml('card-part-detail', data, true, function (responseHtml) {
            $("#modalDetail").html(responseHtml);
            loading.hide();
        });
    }
</script>
<style type="text/css">
    .modal-body {
        overflow: auto;
        max-height: 600px;
    }
</style>

<!-- head filter-->
<div class="panel panel-default">
    <div class="panel-body">
        <div class="form-inline">
            <div class="btn-group col-lg-1">
                <button class="btn btn-primary" type="button" onclick="onAddData()"><i class="fa fa-plus"></i> 新增
                </button>
            </div>
            <div class="pull-right col-lg-11">
                <?php
                if ($current_user_id != '0') {
                    ?>
                    <div class="col-lg-2">
                        <select id="card_type" class="form-control" onchange="">
                            <option value="receive" <?php if ($card_type == "receive") echo "selected=\"selected\""; ?>>
                                收卡管理
                            </option>
                            <option value="send" <?php if ($card_type == "send") echo "selected=\"selected\""; ?>>发卡管理
                            </option>
                        </select>
                    </div>
                    <?php
                }
                ?>

                <?php
                if ($current_institution_code == '0') {
                    ?>
                    <div class="col-lg-2">
                        <div id="from_institution"></div>
                    </div>
                    <?php
                }
                ?>
                <?php
                if ($card_type == 'send') {
                    ?>
                    <div class="col-lg-2">
                        <div id="to_institution"></div>
                    </div>
                    <?php
                }
                ?>


                <div class="col-lg-2">
                    <input type="text" class="form-control" id="card_name" placeholder="卡片名称" value="<?= $name ?>">
                </div>
                <div class="col-lg-1">
                    <input type="text" class="form-control" id="voucher" placeholder="单证号" value="<?= $voucher ?>" style="width: 79px">
                </div>

                <div class="col-lg-2">
                    <div data-date="" class="input-group date form_date">
                        <input type="text" class="form-control" readonly="" size="16" id="startTime"
                               name="startTime" placeholder="开始时间"
                               value="<?= $startTime ?>">
                        <div class="input-group-btn">
                            <button type="button" class="btn btn-default date-reset"><i class="fa fa-times"></i>
                            </button>
                            <button type="button" class="btn btn-default date-set"><i class="fa fa-calendar"></i>
                            </button>
                        </div>
                    </div>
                </div>

                <div class="col-lg-2">
                    <div data-date="" class="input-group date form_date">
                        <input type="text" class="form-control" readonly="" size="16" id="endTime"
                               name="endTime" placeholder="结束时间"
                               value="<?= $endTime ?>">
                        <div class="input-group-btn">
                            <button type="button" class="btn btn-default date-reset"><i class="fa fa-times"></i>
                            </button>
                            <button type="button" class="btn btn-default date-set"><i class="fa fa-calendar"></i>
                            </button>
                        </div>
                    </div>
                </div>
                <div class="col-lg-1">
                    <button type="button" class="btn btn-primary" onclick="filter()"><i class="fa fa-search"></i> 搜索
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- body table-->
<div class="panel panel-default" id="card_list">
    <div class="panel-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-condensed">
                <thead class="nowrap">
                <tr>
                    <th style="width:10%">卡片名称</th>
                    <th style="width:5%">价值积分</th>
                    <th style="width:5%">单证号开始</th>
                    <th style="width:5%">单证号结束</th>
                    <th style="width:10%">发卡机构</th>
                    <th style="width:10%">收卡机构</th>
                    <th style="width:8%">创建时间</th>
                    <th style="width:10%">创建人</th>
                    <th style="width:30%">单证统计</th>
                    <th style="width:7%">操作</th>
                </tr>
                </thead>
                <tbody>
                <?php
                if ($list != null && count($list) > 0) {
                    for ($i = 0; $i < count($list); $i++) {
                        ?>
                        <tr>
                            <td>
                                <button class="btn btn-link btn-xs" type="button"
                                        onclick="getDetail('<?= $list[$i]['id'] ?>')"
                                        data-toggle="modal"
                                        data-target="#myModalDetail">
                                    <?= $list[$i]['name'] ?>
                                </button>
                            </td>
                            <td>
                                <?= $list[$i]['integral'] ?>
                            </td>
                            <td>
                                <?= $list[$i]['start_index'] ?>
                            </td>
                            <td>
                                <?= $list[$i]['end_index'] ?>
                            </td>
                            <td>
                                <?php
                                if ($list[$i]['from_institution_code'] == '0') {
                                    ?>
                                    系统机构
                                    <?php
                                } else {
                                    ?>
                                    <?= $list[$i]['from_institution_name'] ?>
                                    <?php
                                }
                                ?>
                            </td>
                            <td>
                                <?= $list[$i]['to_institution_name'] ?>
                            </td>
                            <td>
                                <?= date('Y-m-d', strtotime($list[$i]['create_datetime'])) ?>
                            </td>
                            <td>
                                <?= $list[$i]['create_manager_name'] ?>
                            </td>
                            <td>
                                <label
                                    class="label label-disable">总数:<?= $list[$i]['end_index'] - $list[$i]['start_index'] + 1 ?></label>
                                <label class="label label-info">已绑定:<?= $list[$i]['s1'] ?></label>
                                <label class="label label-success">已激活:<?= $list[$i]['s2'] ?></label>
                                <label class="label label-danger">已使用:<?= $list[$i]['s3'] ?></label>
                                <label class="label label-warning">失效:<?= $list[$i]['s4'] ?></label>
                                <label class="label label-warning">过期:<?= $list[$i]['s5'] ?></label>
                            </td>
                            <td>
                                <div class="btn-group">
                                    <button class="btn btn-link btn-xs" type="button"
                                            onclick="getCardVoucher('<?= $list[$i]['id'] ?>')"
                                            data-toggle="modal"
                                            data-target="#myModalCardVoucher">
                                        单证
                                    </button>

                                    <?php
                                    if ($list[$i]['from_institution_code'] == $current_institution_code) {
                                        ?>
                                        <button class="btn btn-link btn-xs" type="button"
                                                onclick="onAddData('<?= $list[$i]['id'] ?>')"
                                                data-toggle="modal"
                                                data-target="#myModalEdit">
                                            修改
                                        </button>
                                        <?php
                                    }
                                    ?>
                                </div>
                            </td>
                        </tr>
                        <?php
                    }
                }
                ?>
                </tbody>
            </table>
        </div>
        <div class="pull-left">
            <?= \app\widgets\LinkPagerExt::widget(['pagination' => $pagination]) ?>
        </div>
    </div>
</div>

<!-- Modal card Edit-->
<div class="modal fade" id="myModalEdit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">卡片发送</h4>
            </div>
            <div id="modaledit" class="modal-body">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="saveData()"><i class="fa fa-save"></i> 保存
                </button>
                <button type="button" class="btn btn-primary" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </button>
            </div>
        </div>
    </div>
</div>


<!-- Modal card detail-->
<div class="modal fade" id="myModalDetail" tabindex="-1" role="dialog" aria-labelledby="myModalDetailLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalDetailLabel">卡片详情</h4>
            </div>
            <div id="modalDetail" class="modal-body">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </button>
            </div>
        </div>
    </div>
</div>


<!-- card voucher-->
<div class="modal fade" id="myModalCardVoucher" tabindex="-1" role="dialog" aria-labelledby="myModalCardVoucherLable">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalCardVoucherLable">单证</h4>
            </div>
            <div id="modalCardVoucher" class="modal-body">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </button>
            </div>
        </div>
    </div>
</div>


<!--删除确认对话框-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="deleteModal" class="modal fade">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">是否确认操作？</h4>
                <input type="hidden" id="deleteDataId" class="form-control" value="">
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary" onclick="delData()"><i class="fa fa-ok"></i> 确认</button>
                <button class="btn btn-primary" onclick="delClose()"><i class="fa fa-remove"></i> 取消</button>
            </div>
        </div>
    </div>
</div>

<!--提示对话框-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="hintMessageModal" class="modal fade">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">提示</h4>
            </div>
            <div class="modal-body" id="hint_message" align="center">
            </div>
        </div>
    </div>
</div>
<link rel="stylesheet" type="text/css" href="<?= $web ?>/css/datetimepicker-custom.css"/>
<script src="<?= $web ?>/js/bootstrap-datetimepicker.js"></script>
<script src="<?= $web ?>/js/pickers-init.js"></script>